/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cronos.monitor;

import cronos.application.Application;
import cronos.dao.DAO;
import cronos.metric.Metric;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class MonitorApplicationDAO extends DAO {

    public MonitorApplicationDAO() throws SQLException {
    }

    public List<MonitorApplication> getMonitorApplicationByApplicationId(Integer id) throws SQLException {
        List<MonitorApplication> result = new ArrayList<MonitorApplication>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT a.application_id, a.metric_id, b.name as metric_name, c.name as application_name, a.value, a.\"time\" FROM application_metric_value a, metric b, application c WHERE a.application_id = ? AND a.metric_id = b.id AND a.application_id = c.id ORDER BY a.application_id ASC");
            preparedStatement.setInt(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet != null && resultSet.next()) {
                MonitorApplication monitorApplication = new MonitorApplication();
                
                Application application = new Application();
                application.setId(resultSet.getInt("application_id"));
                application.setName(resultSet.getString("application_name"));
                
                Metric metric = new Metric();
                metric.setId(resultSet.getInt("metric_id"));
                metric.setName(resultSet.getString("metric_name"));
                
                monitorApplication.setApplication(application);
                monitorApplication.setMetric(metric);
                
                monitorApplication.setValue(resultSet.getDouble("value"));
                monitorApplication.setTime(new Date(resultSet.getTimestamp("time").getTime()));
                
                result.add(monitorApplication);
            }
            resultSet.close();
            preparedStatement.close();
            connection.commit();
        } catch (SQLException ex) {
            connection.rollback();
            throw ex;
        }
        return result;
    }
    
    public List<MonitorApplication> getMonitorApplicationInAllApplications() throws SQLException {
        List<MonitorApplication> result = new ArrayList<MonitorApplication>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT a.application_id, a.metric_id, b.name as metric_name, c.name as application_name, a.value, a.\"time\" FROM application_metric_value a, metric b, application c WHERE a.metric_id = b.id AND a.application_id = c.id ORDER BY a.application_id ASC");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet != null && resultSet.next()) {
                MonitorApplication monitorApplication = new MonitorApplication();
                
                Application application = new Application();
                application.setId(resultSet.getInt("application_id"));
                application.setName(resultSet.getString("application_name"));
                
                Metric metric = new Metric();
                metric.setId(resultSet.getInt("metric_id"));
                metric.setName(resultSet.getString("metric_name"));
                
                monitorApplication.setApplication(application);
                monitorApplication.setMetric(metric);
                
                monitorApplication.setValue(resultSet.getDouble("value"));
                monitorApplication.setTime(new Date(resultSet.getTimestamp("time").getTime()));
                
                result.add(monitorApplication);
            }
            resultSet.close();
            preparedStatement.close();
            connection.commit();
        } catch (SQLException ex) {
            connection.rollback();
            throw ex;
        }
        return result;
    }

}